package com.situ.mall.thread.pool;


import java.util.concurrent.*;

public class ThreadPoolDemo2 {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        //10个顾客请求
        try {
            for (int i = 1; i <=10; i++) {
                final int index = i;
                //线程池执行调用execute方法
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName()+" 为第"+index+"个客户办理业务");
                    }
                });
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //线程池的关闭调用shutdown方法
            threadPool.shutdown();
        }
    }
}
